package com.coffeeloversupportlibrary.coffeelover.domain.article.adapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.domain.article.cotegory.CategoryFragmentTest;
import com.coffeeloversupportlibrary.coffeelover.domain.article.cotegory.GameFragment;
import com.coffeeloversupportlibrary.coffeelover.domain.article.type.ArticleCategoriesType;
import com.coffeeloversupportlibrary.coffeelover.uicompont.conflict.NestedScrollingParent2LayoutImpl3;
import com.coffeeloversupportlibrary.coffeelover.domain.article.cotegory.FragmentAdapter;
import com.coffeeloversupportlibrary.databinding.ArticleCotegoryBinding;
import java.util.ArrayList;
import java.util.List;

/**
 * @author puyv
 * @createTime 2021/11/10 11:26
 * @projectName CoffeeLoverSupportLibrary
 * @className ArticleCategoriesAdapter.java
 * @description TODO
 */
public class ArticleCategoriesAdapter implements Adapter<List<ArticleCategoriesType>>{
    private FragmentManager fragmentManager;
    private NestedScrollingParent2LayoutImpl3 nestedScrollingParent2LayoutImpl3;
    private int mSelectedPosition;

    public ArticleCategoriesAdapter(FragmentManager fragmentManager){
        this.fragmentManager=fragmentManager;
    }
    public ArticleCategoriesAdapter(FragmentManager fragmentManager,NestedScrollingParent2LayoutImpl3 nestedScrollingParent2LayoutImpl3){
        this.fragmentManager=fragmentManager;
        this.nestedScrollingParent2LayoutImpl3=nestedScrollingParent2LayoutImpl3;
        //todo : 不初始化的效果
//        this.mSelectedPosition=0;
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.article_cotegory,parent,false);
        ArticleCotegoryBinding articleCotegoryBinding=ArticleCotegoryBinding.bind(view);
        return new ArticleCategoriesViewHolder(articleCotegoryBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull List<ArticleCategoriesType> items, @NonNull RecyclerView.ViewHolder holder, int position) {
        ArticleCategoriesType item=items.get(position);
        List<String> titles=item.getCategoriesTitle();


        List<Fragment> lists=new ArrayList<>();

        // 文章类别 Adapter 持有该位置的 ViewHolder该 ViewHolder 持有 ViewPage viewpage 里面是一系列 Fragment
        //在else 里写 答题 游戏 活动入口
        for (int i = 0; i < titles.size(); i++) {
            ((ArticleCategoriesViewHolder)holder).articleCotegoryBinding
                    .tabTitle.addTab( ((ArticleCategoriesViewHolder)holder)
                    .articleCotegoryBinding.tabTitle.newTab().setText(titles.get(i)));

           if(i!=0){
               CategoryFragmentTest fragment =new CategoryFragmentTest();
               fragment.setType(i);
               fragment.setIndex(i);
               fragment.setNestedParentLayout(this.nestedScrollingParent2LayoutImpl3);
               lists.add(fragment);
           }else{
               GameFragment gameFragment=new GameFragment();
               lists.add(gameFragment);
           }
        }

        FragmentAdapter adapter=new FragmentAdapter(fragmentManager,lists,titles);

        //绑定viewpage的adapter
        ((ArticleCategoriesViewHolder)holder)
                .articleCotegoryBinding
                .articlePage
                .setAdapter(adapter);


        ((ArticleCategoriesViewHolder)holder)
                .articleCotegoryBinding
                .articlePage
                .setCurrentItem(mSelectedPosition);

        ((ArticleCategoriesViewHolder)holder)
                .articleCotegoryBinding
                .articlePage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                    mSelectedPosition=position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        ((ArticleCategoriesViewHolder)holder)
                .articleCotegoryBinding.tabTitle.setupWithViewPager(  ((ArticleCategoriesViewHolder)holder)
                .articleCotegoryBinding.articlePage);


        if (nestedScrollingParent2LayoutImpl3 != null) {
            nestedScrollingParent2LayoutImpl3.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    //设置最后一个item：tab+viewPager
                    nestedScrollingParent2LayoutImpl3.setLastItem( ((ArticleCategoriesViewHolder)holder).articleCotegoryBinding.getRoot());
                }
            });
        }
    }

    static class ArticleCategoriesViewHolder extends RecyclerView.ViewHolder{
        ArticleCotegoryBinding articleCotegoryBinding;
        public ArticleCategoriesViewHolder(@NonNull ArticleCotegoryBinding articleCotegoryBinding) {
            super(articleCotegoryBinding.getRoot());
            this.articleCotegoryBinding=articleCotegoryBinding;
        }
    }
}
